/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import java.util.List;
import models.Employee;
import models.Student;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Achie
 */
public class StudentDao {

    private final Session session;

    public StudentDao(Session session) {
        this.session = session;
    }

    public List<Student> findAll() {
        Transaction tx = session.beginTransaction();
        List<Student> list = session.createQuery("from Student").list();
        tx.commit();
        return list;
    }

    public boolean save(Student student) {
        try {
            Transaction tx = session.beginTransaction();
            session.save(student);
            tx.commit();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean update(Student student) {
        try {
            Transaction tx = session.beginTransaction();
            session.update(student);
            tx.commit();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public Student get(long id) {
        return (Student) session.get(Student.class, id);
    }

    public void delete(Student student) {
        Transaction tx = session.beginTransaction();
        session.delete(student);
        tx.commit();
    }

    //get student objects (list) by student name(firstname, insertion, lastname)
    //this method is used for a search
    public List<Student> findStudentByName(String name) {
        Transaction tx = session.beginTransaction();
        String sql = "select id from Student where firstName ilike :name OR insertion ilike :name OR lastname ilike :name OR firstname||' '||lastname ilike :name OR firstname||' '||insertion||' '||lastname ilike :name";
        Query query = session.createSQLQuery(sql).setParameter("name", "%" + name + "%");
        List idlist = query.list();
        ArrayList<Student> list = new ArrayList<Student>();
        for (int i = 0; i < idlist.size(); i++) {
            Student student = get(Long.parseLong(idlist.get(i).toString()));
            list.add(student);
        }
        System.out.println(list.size());
        tx.commit();
        return list;
    }

    //find all students objects by the students email
    public List<Student> findStudentByEmail(String email) {
        Transaction tx = session.beginTransaction();
        String sql = "select st.id from student st inner join member mm on st.id=mm.id AND mm.email ilike :email";
        Query query = session.createSQLQuery(sql).setParameter("email", "%" + email + "%");
        List idlist = query.list();
        ArrayList<Student> list = new ArrayList<Student>();
        for (int i = 0; i < idlist.size(); i++) {
            Student student = get(Long.parseLong(idlist.get(i).toString()));
            list.add(student);
        }
        System.out.println(list.size());
        tx.commit();
        return list;
    }
}
